limsMaterialApply.editPage = {

    getGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: _this.idManager.materialApplyGrid,
            url: _this.urlManager.materialApplyGrid + '/queries/raw',
            service: 'limsMaterialApplyServiceImpl',
            dbTable: 'T_LIMS_MATERIAL_APPLY',
            columns: limsMaterialApply.getEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                }
            },
            onRowActive: function (index, row) {
            },
            deleteFormatter: function (row) {
                return row.applyCode || row.id
            },
            onCellClick: function (field, row) {
                field === 'applyCode' && _this.loadDetailPage(row);
            },
            requestData: {
                createdById_EQ: workspace.user.id,
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.RM.MATERIALAPPLYS.MODAL.ADD',
                        fields: limsMaterialApply.getBaseInfoFormFields(),
                        url: limsMaterialApply.baseUrl,
                        disableNotEmptyValidate: true,
                    }).done(function (id) {
                        _this.loadDetailPage({id: id})
                        Gikam.getComp(_this.idManager.materialApplyGrid).refresh()
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp(_this.idManager.materialApplyGrid).deleteRows(limsMaterialApply.baseUrl)
                }
            }, {
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                icon: 'submit', color: 'success',
                onClick: function () {
                    _this.submit()
                }
            }]
        }
    },

    loadDetailPage: function (row, readonly) {
        workspace.window.load(limsMaterialApply.baseUrl + '/page/edit-detail' + Gikam.param({
            id: row.id,
            step: 'edit'
        }))
    },

    idManager: {
        materialApplyGrid: 'lims-material-apply-edit-list-grid',
    },

    urlManager: {
        materialApplyGrid: limsMaterialApply.baseUrl,
    },

    submit: function () {
        var grid = Gikam.getComp('lims-material-apply-edit-list-grid')
        Gikam.create('workflow').submit({
            data: grid.getSelections(),
            pageObject: limsMaterialApply,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            grid.refresh()
        })
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-material-apply-edit-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [this.getGridParam()]
            }
        })
    },

    init: function () {
        this.create()
    }
}
